package metaRobot;

import robocode.*;
import robocode.util.Utils;
import java.awt.Color;
import java.awt.geom.Point2D;
		
public class Robot_0_45 extends ACRobot
{
	private Point2D.Double _enemyLocation;
	
	public void run() 
	{
		setColors(Color.black, Color.green, Color.yellow, Color.green, Color.red);
		setAdjustRadarForGunTurn(true);	
		while(true) 
		{
						if (isCornered()) { 
				if ((Math.abs(getBattleFieldWidth() - getX()) < 50) && (getHeading() > 0) && (getHeading() < 180)) { 
					if ((Math.abs(getBattleFieldHeight() - getY()) < 50) && ((getHeading() < 90) || (getHeading() > 270))) { 
						move(false, 50, false, 10);
					} 
					else { 
						move(false, 150, true, 90);
					}
				} 
				else { 
					move(false, 150, true, 10);
				}
			} 
			else { 
				move(true, 50, false, 10);
				if ((getY() < 50) && (getHeading() > 90) && (getHeading() < 270)) { 
					move(false, 50, false, 10);
				} 
				else { 
					turnPerpendicularToNearestWall();
				}
			}

			
			execute();
		}
	}
	
	public void onScannedRobot(ScannedRobotEvent e) 
	{
		// aggiorna la posizione del nemico
		double absBearing = e.getBearingRadians() + getHeadingRadians();
		_enemyLocation = project(new Point2D.Double(getX(), getY()), absBearing, e.getDistance());
		
		if (testEnemyWithinTicksOfFire(e, 5, 3)) { 
			turnGunRight(5);
		} 
		else { 
			turnGunToEnemy(_enemyLocation);
		}


		execute();
	}
	
	public void onHitByBullet(HitByBulletEvent e) 
	{
		move(false, 50, false, 90);
		if (testEnemyWithinTicks(_enemyLocation, 30)) { 
			setTurnRadarLeft(60);
		} 
		else { 
			move(false, 50, true, 90);
		}


		execute();
	}

	public void onHitWall(HitWallEvent e) 
	{
		move(true, 50, false, 10);


		execute();
	}
	
	public void onHitRobot(HitRobotEvent e) 
	{
		moveToLowRight();


		execute();
	}
}